package com.scalar.chiptrack.operations.reports.actions;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.ScalarException;
import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.utils.WorkWeek;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.operations.reports.dao.WipFlushDAO;
import com.scalar.chiptrack.operations.reports.forms.WipFlushForm;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.operations.orderscheduling.dao.OrderSchedulingDAO;
import com.scalar.chiptrack.users.User;

/**
 * Created by IntelliJ IDEA.
 * Author: Basha
 * Date: Jun 10, 2004
 * Time: 12:37:55 PM
 * To change this template use Options | File Templates.
 */

public class WipFlushAction extends ChipTrackAction {

    private static final String ACTION_NAME = "/wipFlush.do" ;

    public ActionForward execute (ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response)
                                     throws Exception {

        HttpSession session = request.getSession();
        User user = (User)session.getAttribute (SessionKeys.USER_KEY);

        if (user == null) {
            session.setAttribute (SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString());
            return (mapping.findForward ("login"));
        }

        WipFlushForm subForm = (WipFlushForm) form;

        HashMap errors 	= new HashMap();
        HashMap info 	= new HashMap();

        String mainGroup 	= subForm.getMainGroup();
        String level 		= subForm.getLevel();
        String workWeek 	= subForm.getWorkWeek();
        String allocation 	= subForm.getAllocation();
        String holdLot 		= subForm.getHoldLot();
        int noOfWeeks		= subForm.getNoOfWeeks();
        String action       = request.getParameter("action");
        boolean success     = false;

        String actionForward = "success";

        request.setAttribute( "baseProducts", NewProductDAO.loadAllBaseProducts( null, true, true ) );
        ArrayList mktPartNumberList = NewProductDAO.loadMarketingPartNumbers( null, "FG" );
        request.setAttribute( "mktPartNumberList",  mktPartNumberList);
        
        if( action != null && "CalculateWipFlush".equalsIgnoreCase( action ) )
        {
            try
            {
                WipFlushDAO.calculateWipFlushQuantities( null, 0 );
                //RE ALLOCATING ALL SCHEDULED LINE ITEMS
                OrderSchedulingDAO.reAllocateAllBackLog(null, null);
                success = true;
            }
            catch ( ScalarException se )
            {
                se.printStackTrace();
                errors.put ("Could not Calculate Wip Flush", null);
                success = false;
            }

            if( success )
            {
                info.put ( "Wip Flush calculations successfully done", null );
            }
            if ( ! info.isEmpty () )
            {
                request.setAttribute ( SessionKeys.INFO_MESSAGES, info );
            }
            request.setAttribute ( SessionKeys.ERROR_MESSAGES, errors );
            return ( mapping.findForward( actionForward ) );
        }

        String holdHeading = "";
        if( holdLot != null )
        {
            if( "2".equals( holdLot) )
                holdHeading = "Without Hold";
            else if ( "3".equals( holdLot ) )
                holdHeading = "With Hold";

        }
        request.setAttribute( "holdHeading", holdHeading );
        if (mainGroup == null && level == null)
        {
            actionForward = "success";
        }
        else
        {
            try
            {
                if ("product".equalsIgnoreCase(mainGroup))
                {
                    if ("weekDate".equalsIgnoreCase(workWeek))
                    {
                        WorkWeek workWeekObj = WorkWeek.getCurrentWorkWeekInstance();
                        int wwNumber = workWeekObj.getCurrentWWNumber();
                        int wwYear = workWeekObj.getWorkWeekYear();
                        request.setAttribute ( "startingWWNumber", Integer.toString ( wwNumber ) );
                        request.setAttribute( "wwYear", Integer.toString ( wwYear ) );
                        if( "shippable".equalsIgnoreCase( allocation ) )
                        {
                            //request.setAttribute( "wipFlush", WipFlushDAO.getShippableUnitsByWeek( 0, 0, 2, holdLot, noOfWeeks ) );
                            session.setAttribute( "wipFlush", WipFlushDAO.getShippableUnitsByWeek( subForm.getBaseProductIds(), 0, 2, holdLot, workWeekObj.getCurrentWWStartDate(), subForm.getFgProcessStepIds()) );
                            request.setAttribute( "heading", "Shippable Units" );
                        }
                        else if( "unallocated".equalsIgnoreCase( allocation ) )
                        {
                            //request.setAttribute( "wipFlush", WipFlushDAO.getShippableUnitsByWeek( 0, 0, 3, holdLot, noOfWeeks ) );
                            request.setAttribute( "wipFlush", WipFlushDAO.getShippableUnitsByWeek( subForm.getBaseProductIds(), 0, 3, holdLot, workWeekObj.getCurrentWWStartDate(), subForm.getFgProcessStepIds()) );
                            request.setAttribute( "heading", "Unallocated Units" );
                        }
                        else if( "allocated".equalsIgnoreCase( allocation ) )
                        {
                            //request.setAttribute( "wipFlush", WipFlushDAO.getShippableUnitsByWeek( 0, 0, 4, holdLot, noOfWeeks ) );
                            request.setAttribute( "wipFlush", WipFlushDAO.getShippableUnitsByWeek( subForm.getBaseProductIds(), 0, 4, holdLot, workWeekObj.getCurrentWWStartDate(), subForm.getFgProcessStepIds()) );
                            request.setAttribute( "heading", "Allocated Units" );
                        }
                        request.setAttribute( "noOfWeeks", noOfWeeks+"" );
                        actionForward = "productByWeek";
                    }
                    else if ("monthDate".equalsIgnoreCase(workWeek))
                    {
                        if ("shippable".equalsIgnoreCase(allocation))
                        {
                            session.setAttribute("wipFlush",WipFlushDAO.getShippableUnitsByMonth(subForm.getBaseProductIds(),0,2,holdLot, subForm.getFgProcessStepIds()));
                            request.setAttribute("heading","Shippable Units");
                        }
                        else if ("unallocated".equalsIgnoreCase(allocation))
                        {
                            request.setAttribute("wipFlush",WipFlushDAO.getShippableUnitsByMonth(subForm.getBaseProductIds(),0,3,holdLot, subForm.getFgProcessStepIds()));
                            request.setAttribute("heading","Unallocated Units");
                        }
                        else if ("allocated".equalsIgnoreCase(allocation))
                        {
                            request.setAttribute("wipFlush",WipFlushDAO.getShippableUnitsByMonth(subForm.getBaseProductIds(),0,4,holdLot, subForm.getFgProcessStepIds()));
                            request.setAttribute("heading","Allocated Units");
                        }

                        actionForward = "productByMonth";
                    }
                    else
                    {
                        ArrayList wipInventoryByProduct = null;
                        String heading = "";
                        if ("all".equalsIgnoreCase(allocation))
                        {
                            wipInventoryByProduct   = WipFlushDAO.getWipInventoryByProduct(subForm.getBaseProductIds(),0,1,holdLot, subForm.getFgProcessStepIds());
                            session.setAttribute("wipFlush",wipInventoryByProduct );
                            heading = "Inventory";
                            request.setAttribute("heading", heading );
                        }
                        else if ("shippable".equalsIgnoreCase(allocation))
                        {
                            wipInventoryByProduct   = WipFlushDAO.getWipInventoryByProduct(subForm.getBaseProductIds(),0,2,holdLot, subForm.getFgProcessStepIds());
                            session.setAttribute("wipFlush", wipInventoryByProduct );
                            heading = "Shippable Units";
                            request.setAttribute("heading", heading );
                        }
                        else if ("unallocated".equalsIgnoreCase(allocation))
                        {
                            wipInventoryByProduct   = WipFlushDAO.getWipInventoryByProduct(subForm.getBaseProductIds(),0,3,holdLot, subForm.getFgProcessStepIds());
                            session.setAttribute("wipFlush", wipInventoryByProduct );
                            heading = "Unallocated Units";
                            request.setAttribute("heading", heading );
                        }
                        else if ("allocated".equalsIgnoreCase(allocation))
                        {
                            wipInventoryByProduct   = WipFlushDAO.getWipInventoryByProduct(subForm.getBaseProductIds(),0,4,holdLot, subForm.getFgProcessStepIds());
                            session.setAttribute("wipFlush", wipInventoryByProduct);
                            heading = "Allocated Units";
                            request.setAttribute("heading", heading);
                        }
                         actionForward = "product";
                    }

                    if("expandOneLevel".equalsIgnoreCase(level))
                    {
                        request.setAttribute("vendorExpand","display=block");
                        request.setAttribute("lotExpand","display=none");
                    }
                    else if ("expandAll".equalsIgnoreCase(level))
                    {
                        request.setAttribute("vendorExpand","display=block");
                        request.setAttribute("lotExpand","display=block");
                    }
                    else if ("collapseAll".equalsIgnoreCase(level))
                    {
                        request.setAttribute("vendorExpand","display=none");
                        request.setAttribute("lotExpand","display=none");
                    }
                }
                else if ("vendor".equalsIgnoreCase(mainGroup))
                {
                    if ("weekDate".equalsIgnoreCase(workWeek))
                    {
                        WorkWeek workWeekObj = WorkWeek.getCurrentWorkWeekInstance();
                        int wwNumber = workWeekObj.getCurrentWWNumber();
                        int wwYear = workWeekObj.getWorkWeekYear();
                        request.setAttribute ( "startingWWNumber", Integer.toString ( wwNumber ) );
                        request.setAttribute( "wwYear", Integer.toString ( wwYear ) );
                        if ("shippable".equalsIgnoreCase(allocation))
                        {
                            //request.setAttribute( "wipFlush", WipFlushDAO.getShippableUnitsByWeekByVendor( 0, 0, 2, holdLot, noOfWeeks ) );
                            session.setAttribute( "wipFlush", WipFlushDAO.getShippableUnitsByWeekByVendor( subForm.getBaseProductIds(), 0, 2, holdLot, workWeekObj.getCurrentWWStartDate(), subForm.getFgProcessStepIds()) );
                            request.setAttribute("heading","Shippable Units");
                        }
                        else if ("unallocated".equalsIgnoreCase(allocation))
                        {
                            //request.setAttribute( "wipFlush", WipFlushDAO.getShippableUnitsByWeekByVendor( 0, 0, 3, holdLot, noOfWeeks ) );
                            request.setAttribute( "wipFlush", WipFlushDAO.getShippableUnitsByWeekByVendor( subForm.getBaseProductIds(), 0, 3, holdLot, workWeekObj.getCurrentWWStartDate(), subForm.getFgProcessStepIds()) );
                            request.setAttribute("heading","Unallocated Units");
                        }
                        else if ("allocated".equalsIgnoreCase(allocation))
                        {
                            //request.setAttribute( "wipFlush", WipFlushDAO.getShippableUnitsByWeekByVendor( 0, 0, 4, holdLot, noOfWeeks ) );
                            request.setAttribute( "wipFlush", WipFlushDAO.getShippableUnitsByWeekByVendor( subForm.getBaseProductIds(), 0, 4, holdLot, workWeekObj.getCurrentWWStartDate(), subForm.getFgProcessStepIds()) );
                            request.setAttribute("heading","Allocated Units");
                        }
                        request.setAttribute( "noOfWeeks", noOfWeeks+"" );
                        actionForward = "vendorByWeek";
                    }
                    else if ("monthDate".equalsIgnoreCase(workWeek))
                    {
                        if ("shippable".equalsIgnoreCase(allocation))
                        {
                            session.setAttribute("wipFlush",WipFlushDAO.getShippableUnitsByMonthByVendor(subForm.getBaseProductIds(),0,2,holdLot, subForm.getFgProcessStepIds()));
                            request.setAttribute("heading","Shippable Units");
                        }
                        else if ("unallocated".equalsIgnoreCase(allocation))
                        {
                            request.setAttribute("wipFlush",WipFlushDAO.getShippableUnitsByMonthByVendor(subForm.getBaseProductIds(),0,3,holdLot, subForm.getFgProcessStepIds()));
                            request.setAttribute("heading","Unallocated Units");
                        }
                        else if ("allocated".equalsIgnoreCase(allocation))
                        {
                            request.setAttribute("wipFlush",WipFlushDAO.getShippableUnitsByMonthByVendor(subForm.getBaseProductIds(),0,4,holdLot, subForm.getFgProcessStepIds()));
                            request.setAttribute("heading","Allocated Units");
                        }

                        actionForward = "vendorByMonth";
                    }
                    else
                    {
                        ArrayList wipInventoryByVendor  = null;
                        String heading  = "";
                        if ("all".equalsIgnoreCase(allocation))
                        {
                            wipInventoryByVendor = WipFlushDAO.getWipInventoryByVendor(subForm.getBaseProductIds(),0,1,holdLot, subForm.getFgProcessStepIds());
                            session.setAttribute("wipFlush", wipInventoryByVendor);
                            heading = "Inventory";
                            request.setAttribute("heading", heading );
                        }
                        else if ("shippable".equalsIgnoreCase(allocation))
                        {
                            wipInventoryByVendor = WipFlushDAO.getWipInventoryByVendor(subForm.getBaseProductIds(),0,2,holdLot, subForm.getFgProcessStepIds());
                            session.setAttribute("wipFlush", wipInventoryByVendor );
                            heading = "Shippable Units";
                            request.setAttribute("heading", heading );
                        }
                        else if ("unallocated".equalsIgnoreCase(allocation))
                        {
                            wipInventoryByVendor = WipFlushDAO.getWipInventoryByVendor(subForm.getBaseProductIds(),0,3,holdLot, subForm.getFgProcessStepIds());
                            session.setAttribute("wipFlush", wipInventoryByVendor );
                            heading = "Unallocated Units";
                            request.setAttribute("heading", heading );
                        }
                        else if ("allocated".equalsIgnoreCase(allocation))
                        {
                            wipInventoryByVendor = WipFlushDAO.getWipInventoryByVendor(subForm.getBaseProductIds(),0,4,holdLot, subForm.getFgProcessStepIds());
                            session.setAttribute("wipFlush", wipInventoryByVendor );
                            heading = "Allocated Units";
                            request.setAttribute("heading", heading);
                        }
                        actionForward = "vendor";
                    }

                    if("expandOneLevel".equalsIgnoreCase(level))
                    {
                        request.setAttribute("productExpand","display=block");
                        request.setAttribute("lotExpand","display=none");
                    }
                    else if ("expandAll".equalsIgnoreCase(level))
                    {
                        request.setAttribute("productExpand","display=block");
                        request.setAttribute("lotExpand","display=block");
                    }
                    else if ("collapseAll".equalsIgnoreCase(level))
                    {
                        request.setAttribute("productExpand","display=none");
                        request.setAttribute("lotExpand","display=none");
                    }
                }
            }
            catch (ScalarException se) {
                errors.put ("Internal Server Error", "Could not get WIP information");
                errors.put( ERROR_MESSAGE_CONTACT_SYS_ADMN, null );
                session.setAttribute( SessionKeys.ERROR_REPORT, se );
            }

            if ( !errors.isEmpty () ) {
                request.setAttribute ( SessionKeys.ERROR_MESSAGES, errors );
            }
            if ( !info.isEmpty () ) {
                request.setAttribute ( SessionKeys.INFO_MESSAGES, info );
            }

        }

        return (mapping.findForward (actionForward));

    } //  End of "execute" method
}